package com.srwl.mytx.activity;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;

import androidx.fragment.app.FragmentTransaction;

import com.srwl.mytx.fragment.CouponReceivedRecordFragment;
import com.srwl.mytx.fragment.CouponSendRecordFragment;
import com.srwl.mytx.widget.ChooseRecordPopupWindow;
import com.srwl.mytx.widget.RPTitleBar;
import com.srwl.mytx.R;


public class CouponRecordActivity extends BaseActivity {

    private ChooseRecordPopupWindow mPopupWindow;

    private ProgressBar mProgressBar;

    private RPTitleBar mTitleBar;
    private static int OPEN_RECEIVE_FRAGMENT_FLAG = 1;

    private int offsetY;


    public static final String EXTRA_OPEN_FRAGMENT_FLAG = "open";
    int openFragmentFlag;
    private CouponReceivedRecordFragment couponReceivedRecordFragment;
    private CouponSendRecordFragment couponSendRecordFragment;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.rp_activity_record);
        initViewsAndEvents();
    }
    //必须重新，不然状态栏字体是黑色，，
    protected void setStatusDark(){
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.rp_top_red_color));
    }


    protected void initViewsAndEvents() {
        //默认打开收入Fragment
        openFragmentFlag = getIntent().getIntExtra(EXTRA_OPEN_FRAGMENT_FLAG, OPEN_RECEIVE_FRAGMENT_FLAG);
        final int dp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 7, getResources().getDisplayMetrics());
        mProgressBar = findViewById(R.id.progressBar1);
        initTitleBar(dp);
        if (openFragmentFlag == OPEN_RECEIVE_FRAGMENT_FLAG) {
            showReceiveFragment();
        } else {
            showSendFragment();
        }


    }

    private void initTitleBar(final int dp) {
        mTitleBar = findViewById(R.id.title_bar);
        mTitleBar.setSubTitleVisibility(View.GONE);
        mTitleBar.setRightImageResource(R.drawable.tx_right_menu_yellow);
        mTitleBar.setRightImageLayoutVisibility(View.VISIBLE);
        mTitleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // mTitleBar.setRightTextLayoutVisibility(View.VISIBLE);
        mTitleBar.setRightImageLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                if (!TextUtils.isEmpty(mTitleBar.getTitleText())) {
//                    //当前是支出，就打开收入Fragment
//                    if (mTitleBar.getTitleText().equals(getString(R.string.money_send_text))) {
//                        openReceiveFragment();
//
//                    } else {
//                        showSendFragment();
//                    }
//                }


                if (mPopupWindow == null) {
                    mPopupWindow = new ChooseRecordPopupWindow(CouponRecordActivity.this, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {


                            if (v.getId() == R.id.tv_choose_send) {
                                showSendFragment();
                            }
                            if (v.getId() == R.id.tv_choose_received) {
                                showReceiveFragment();
                            }
                            mPopupWindow.dismiss();
                        }
                    });
                    int[] location = new int[2];
                    mTitleBar.getLocationOnScreen(location);
                    offsetY = location[1] + mTitleBar.getHeight();
                }
                mPopupWindow.showAtLocation(mTitleBar, Gravity.END | Gravity.TOP, dp, offsetY + dp);
            }
        });
    }

    private void showSendFragment() {
        if (couponSendRecordFragment == null) {
            couponSendRecordFragment = CouponSendRecordFragment.newInstance();
        } else if (!couponSendRecordFragment.isHidden()) {
            return;
        }
        mTitleBar.setTitle("发出的红包");
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (couponReceivedRecordFragment != null && !couponReceivedRecordFragment.isHidden()) {
            fragmentTransaction.hide(couponReceivedRecordFragment);
        }
        if (!couponSendRecordFragment.isAdded()) {
            fragmentTransaction.add(R.id.record_fragment_container, couponSendRecordFragment);
        }

        fragmentTransaction.show(couponSendRecordFragment).commit();

    }

    private void showReceiveFragment() {
        if (couponReceivedRecordFragment == null) {
            couponReceivedRecordFragment = CouponReceivedRecordFragment.newInstance();
        } else if (!couponReceivedRecordFragment.isHidden()) {
            return;
        }
        mTitleBar.setTitle("收到的红包");
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (couponSendRecordFragment != null && !couponSendRecordFragment.isHidden()) {
            fragmentTransaction.hide(couponSendRecordFragment);
        }

        if (!couponReceivedRecordFragment.isAdded()) {
            fragmentTransaction.add(R.id.record_fragment_container, couponReceivedRecordFragment);
        }
        fragmentTransaction.show(couponReceivedRecordFragment).commit();


    }


    public void hideProgressBar() {
        if (mProgressBar.getVisibility() == View.VISIBLE) {
            mProgressBar.setVisibility(View.GONE);
        }
    }

    public void showProgressBar() {
        if (mProgressBar.getVisibility() == View.GONE) {
            mProgressBar.setVisibility(View.VISIBLE);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
